<script setup>
import { getUserAPI, postUserAPI } from '@/API/user.js'
import { ref, onMounted, nextTick } from 'vue';
import { useTheUserStore } from '@/stores/data/the_user.js'
import { useRouter } from "vue-router";
const router = useRouter()
const useUser = useTheUserStore()
const isLogin = ref(false)

// 注册逻辑
const handle = async () => {
	if (!ruleForm.value.user?.trim()) return
	if (!ruleForm.value.passwd?.trim()) return
	await ruleFormRef.value.validate(async (valid) => {
		if (valid) {
			const checkResponse = await getUserAPI(ruleForm.value.user, ruleForm.value.passwd)
			if (checkResponse.data.length > 0) {
				ElMessage({
					message: '已经有这个账号了',
					type: 'warning'
				})
				ruleForm.value = {}
				return
			} else {
				ElMessage({
					message: '创建成功',
					type: 'success'
				})
				isLogin.value = !isLogin.value
				const res = await postUserAPI(ruleForm.value.user, ruleForm.value.passwd)
				// 创建成功就加入到本地存储
				useUser.setUserInfo(ruleForm.value.user, ruleForm.value.passwd)
			}

		} else {
			console.log('验证失败');
			return false;
		}
	});
	console.log(ruleForm.value.user, ruleForm.value.passwd);
	// get里面传参是匹配有这种值的数据
	// 这里面的user.value和passwd.value就是匹配的数据，有就找得到

}
// 登录逻辑
const login = async () => {
	if (!ruleForm.value.user?.trim()) return
	if (ruleForm.value.user, ruleForm.value.passwd === '') return
	const checkResponse = await getUserAPI(ruleForm.value.user, ruleForm.value.passwd)
	if (checkResponse.data.length > 0) {
		ElMessage({
			message: '跳转成功',
			type: 'success'
		})
		useUser.setUserInfo(ruleForm.value.user, ruleForm.value.passwd)
		router.push('/')
		return
	} else {
		ruleForm.value.passwd = ''
		ElMessage({
			message: '账号或密码错误',
			type: 'error',
		})

	}
}
// form表单
const ruleFormRef = ref()
// :model匹配的是userInfo数据
// ：rules匹配的是规则
// prop 匹配的是rules里的哪一个规则
// v-model匹配的是ruleform的数据
const ruleForm = ref({
	user: '',
	passwd: ''
})
const rules = {
	user: [
		{ required: true, message: '用户名不能为空', trigger: 'blur' },
		{ min: 9, max: 11, message: '请输入正确的账号', trigger: 'blur' },
	],
	passwd: [
		{ required: true, message: '密码不能为空', trigger: 'blur' },
		{ min: 8, max: 20, required: true, message: '密码要在8-20位', trigger: 'blur' },
		{
			validator: (rule, val, callback) => {
				if (isLogin === true) {
					const passwordReg = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,20}$/
					if (!val) {
						callback()
						return
					}
					if (!passwordReg.test(val)) {
						callback(new Error('至少包含大小写字母和数字，8-20位'));
					} else {
						callback();
					}
				} else {
					callback()
				}
			},
			trigger: ['blur', 'change']
		}
	]
}
const switchVariable = () => {
	isLogin.value = !isLogin.value
	ruleForm.value = {}
}
const handleEnter = async () => {

	await login()
}
</script>
<template>
	<div class="sun"></div>


	<div class="the_top">
		<div class="tit" style="font-size: 20px;"> {{ isLogin === false ? '登录界面' : '注册界面' }} </div>
		<el-form class="the_form" ref="ruleFormRef" style="max-width: 600px" :model="ruleForm" :rules="rules"
			label-width="auto">
			<el-form-item class="cent" label="账号" prop="user">

				<el-input suffix-icon="User" placeholder="请输入账号" v-model="ruleForm.user" />
			</el-form-item>
			<el-form-item class="cent" label="密码" prop="passwd">
				<el-input @keydown.enter="handleEnter" type="password" suffix-icon="Hide" placeholder="请输入密码"
					v-model="ruleForm.passwd" />
			</el-form-item>
			<el-form-item class="cent">
				<el-button @click="handle" v-if="isLogin" type="success">注册</el-button>
				<el-button @click="login" v-else type="success">登录</el-button>
			</el-form-item>
			<div style="font-size: 12px; color: rgb(190, 187, 184);">
				<span v-if="isLogin">还没有账号?</span>
				<a class="zhuce" href="javascript;" @click.prevent="switchVariable">点击去{{ isLogin === true ? '登录' :
					'注册' }}</a>
			</div>
		</el-form>
	</div>


</template>



<style scoped>
.el-input__suffix {
	display: flex;
	/* 默认隐藏 */
}

@keyframes move {
	0% {
		top: 100px;
		left: 0%;
		background-color: red;
		box-shadow: 0px 0px 100px #FFFF00;
	}

	25% {
		top: 50px;

	}

	50% {
		top: 0px;
	}

	75% {
		top: 50px;
	}

	100% {
		top: 100px;
		left: 90%;
		box-shadow: 0px 0px 100px white;
		background-color: black;
		box-shadow: 0px 0px 100px black;
	}
}

.the_top {
	margin: 0 auto;
	width: 500px;
	height: 300px;
	background-color: pink;
	text-align: center;
	margin-top: 350px;
	border-radius: 12px;
}

.tit {
	padding-top: 10px;
}

.the_form {
	width: 500px;
	height: 250px;
}

.user {
	width: 250px;
	height: 40px;
	border-radius: 12px;
	border-color: aqua;
}

.reset {
	width: 80px;
	height: 25px;
	border-radius: 12px;
}

.reset1 {
	margin-right: 15px;
}

.reset2 {
	margin-left: 15px;
}

.cent {
	width: 500px;
	line-height: 50px;
	margin-top: 15px;
}

.zhuce {
	color: aqua;
}

input[type="number"] {
	-moz-appearance: textfield;
}

input[type="number"]::-webkit-outer-spin-button,
input[type="number"]::-webkit-inner-spin-button {
	-webkit-appearance: none;
	margin: 0;
}

.sun {
	width: 10em;
	height: 10em;
	top: 0;
	position: absolute;
	border-radius: 50%;
	box-shadow: 0px 0px 100px #FFFF00;
	animation: move 10s linear infinite;
}

.el-button {
	padding: 15px 35px;
}

.el-form-item__content {
	justify-content: center;
	align-content: space-around;
	align-items: baseline;
}

.el-form-item {
	flex-direction: column;
	flex-wrap: wrap;
	align-content: center;
}
</style>